package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.MyFragmentPagerAdapter;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PopulationFixedBean;
import com.sx.zongzhi.bean.PopulationFixedDetailBean;
import com.sx.zongzhi.fragment.BasicInforFixedFragment;
import com.sx.zongzhi.fragment.FixedBusinessFragment;
import com.sx.zongzhi.presenter.PopFixedDetailPest;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPopFixedDetailView;
import com.sx.zongzhi.widget.CustomDialog;
import com.sx.zongzhi.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 常住人口详情
 */
public class FixedDetailActivity extends BaseActivity implements IPopFixedDetailView {

    @BindView(R.id.tl_TabLayout)
    TabLayout tl_TabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private List<Fragment> fragments=null;
    private String fixedId="";
    private String orgId="";
    private PopulationFixedDetailBean detailBean;
    private PopulationFixedBean.PermissionBean permissionBean;
    private BasicInforFixedFragment basicInforFixedFragment;
    private FixedBusinessFragment fixedBusinessFragment;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_household_detail);
        setTitleName("留守人口详情");
        fixedId=getIntent().getStringExtra("FixedId");
        permissionBean=(PopulationFixedBean.PermissionBean) getIntent().getSerializableExtra("PermissionBean");
        orgId= getIntent().getStringExtra("orgId");
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        LoadDialog.show(this);
        PopFixedDetailPest detailPest=new PopFixedDetailPest(this);
        detailPest.getPopFixedDetail(fixedId);
    }

    private void initView() {
        if(permissionBean!=null){
            if (permissionBean.isDeleteLeftBehindPeople()){
                setRightImage(R.mipmap.icon_nav_delete);
            }
            if (permissionBean.isUpdateLeftBehindPeople()){
                setRightImage2(R.mipmap.icon_nav_edit);
            }
        }
        List<String> titles=new ArrayList<>();
        titles.add("基本信息");
        titles.add("业务信息");
        Bundle bundle=new Bundle();
        bundle.putSerializable("PopulationFixedDetailBean",detailBean);
        basicInforFixedFragment=new BasicInforFixedFragment();
        basicInforFixedFragment.setArguments(bundle);
        fixedBusinessFragment=new FixedBusinessFragment();
        fixedBusinessFragment.setArguments(bundle);
        fragments=new ArrayList<>();
        fragments.add( basicInforFixedFragment);
        fragments.add(fixedBusinessFragment);
        tl_TabLayout.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(0)));
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(1)));
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(2);
        //tabLayout加载viewpager
        tl_TabLayout.setupWithViewPager(viewPager);
    }


    @OnClick({R.id.imv_Right,R.id.imv_Right2})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.imv_Right:
                createDelete();
                break;
            case R.id.imv_Right2:
                if (detailBean!=null){
                    startActivity(new Intent(this,AddFixedActivity.class)
                            .putExtra("PopulationFixedDetailBean",detailBean)
                            .putExtra("orgId",orgId)
                            .putExtra("isEdit",true));
                    finish();
                }else {
                    ToastUtils.showToast(this,"获取详情失败");
                }
                break;

        }
    }

    private void createDelete() {
        CustomDialog.Builder builder=new CustomDialog.Builder(this);
        CustomDialog dialog=builder.setMessage("确定删除此用户吗？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                PopFixedDetailPest detailPest=new PopFixedDetailPest(FixedDetailActivity.this);
                detailPest.deleteFixed(fixedId);
                LoadDialog.show(FixedDetailActivity.this,"删除中...");
            }
        }).create();
        dialog.show();
    }

    @Override
    public void getFixedDetail(PopulationFixedDetailBean detailBean) {
        LoadDialog.dismiss(this);
        if (detailBean!=null){
            this.detailBean=detailBean;
        }
        initView();
    }

    @Override
    public void deleteFixed(boolean isSuccess) {
        LoadDialog.dismiss(this);
        if (isSuccess){
            MyRxBus.get().post(new MsgEvent<>("FixedActivity",2,"删除成功"));
            finish();
        }else {
            ToastUtils.showToast(this,"删除失败");
        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
        if (fragments==null){
            initView();
        }
    }
}
